Skip to content

Comments

Add (some level of) SDM support#832

Closed
playfulFence wants to merge 2 commits intoesp-rs:mainfrom
playfulFence:read_flash
Closed

Add (some level of) SDM support#832
playfulFence wants to merge 2 commits intoesp-rs:mainfrom
playfulFence:read_flash

Conversation

@playfulFence
Copy link
Member

closes #726

Initial issue contains just a little bit of lie: espflash didn't work with SDM almost at all 😅
After this is merged, at least board-info and write-bin (mentioned in the original issue) functions will be supported.

No extra flags are needed, user will be able to just execute:
espflash write-bin 0x0 --monitor <bin-name>

I recommend setting a higher baudrate as otherwise flashing in SDM takes a lot of time.
Also, --no-stub is applied automatically when tool detects that connected chip is in SDM mode.

Copy link
Member

@MabezDev MabezDev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, I haven't tested it, but I'm sure it works :D. I've added some comments which I think will improve the PR.

4
};

let status_len =
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could benefit from some comments, and perhaps a permalink to either the esptool impl, or the docs explaining this return data from the ROM bootloader.

pub features: Vec<String>,
/// MAC address
pub mac_address: String,
pub mac_address: Option<String>,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The ROM bootloader doesn't send MAC in SDM?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, the MAC is read from efuse, but in SDM you can't read efuses, so that's the reason. I should probably add a comment to clarify this though, thanks!

}
}

fn detect_sdm(connection: &mut Connection) -> Result<bool, Error> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we detect sd mode in the normal detection routine, instead of doing it twice?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I.e, inside connection.begin(), can we return the download mode type there?

if use_stub {
info!("Using flash stub");
flasher.load_stub()?;
if !sdm {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you bring the !sdm if outside, you can avoid two seperate if's here and below

_segment: Segment<'_>,
_progress: &mut Option<&mut dyn ProgressCallbacks>,
) -> Result<(), Error> {
todo!()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we can't implement this, this should return a nice error, not panic.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If it can be implemented at some point, please also add a TODO with a relevant issue.


let revision = Some(target.chip_revision(self.connection())?);
// chip_revision reads from efuse, which is not possible in Secure Download Mode
let revision = if !self.connection.secure_download_mode {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can use https://doc.rust-lang.org/std/primitive.bool.html#method.then_some to remove the need for the if statements.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@MabezDev
Not "then_some", as it eagerly evaluates arguments inside of it, which means, that even if chip IS in Secure Download Mode, "read_reg" inside of that "chip_revision" function will be evaluated -> everything will fail.

Instead, me might want to use something like

let revision = (!self.connection.secure_download_mode)
            .then(|| target.chip_revision(self.connection()))
            .transpose()?;

Copy link
Member

@jessebraham jessebraham left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with all of the other review comments, plus a couple more to add. Also, obviously need to get CI green still as well.

CHANGELOG.md Outdated
- Make the default flashing frequency target specific (#389)
- Add note about permissions on Linux (#391)
- Add a diagnostic to tell the user about the partition table format (#397)
- Add (some level of) SDM support (#832)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be nice to have some details here, not really sure what this means TBH.

} else {
Command::SpiAttach {
spi_params: self.spi_attach_params,
if connection.secure_download_mode {
Copy link
Member

@jessebraham jessebraham Apr 7, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be cleaner to just use else if here rather than nested if statements, IMO.

@playfulFence playfulFence marked this pull request as draft April 8, 2025 13:43
@playfulFence
Copy link
Member Author

I've found a great bug there, will get back to it later

@playfulFence
Copy link
Member Author

Thank you @MabezDev and @jessebraham for the reviews though, will address them when I'm back to this task.

playfulFence added a commit to playfulFence/espflash that referenced this pull request Apr 9, 2025
playfulFence added a commit to playfulFence/espflash that referenced this pull request Apr 9, 2025
playfulFence added a commit to playfulFence/espflash that referenced this pull request Apr 9, 2025
github-merge-queue bot pushed a commit that referenced this pull request Apr 10, 2025
* Cut off the write_bin part from original #832

* Changelog entry

* Akela missed...

* Dumb

* Address reviews
fmt

clean

yeah
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

espflash write-bin not compatible with Secure Download mode

3 participants